package cn.elwy.rcp.ui.handler;

import org.eclipse.swt.widgets.Shell;

/**
 * 从下方弹出窗口，并在指定时间后自动关闭
 * 
 * @author huangsq
 * @version 1.0, 2013-11-20
 * @since 1.0, 2013-11-20
 */
public class AutoCloseShell extends Thread {

  private Shell shell;
  private int posX;
  private int posY;
  private int closeTime;

  public AutoCloseShell(Shell shell) {
    this.shell = shell;
  }

  /**
   * 自动关闭窗口时间，为0不关闭
   * 
   * @param shell
   * @param closeTime 秒数
   */
  public AutoCloseShell(Shell shell, int closeTime) {
    this.shell = shell;
    this.closeTime = closeTime;
  }

  public void run() {
    shell.getDisplay().asyncExec(new Runnable() {
      @Override
      public void run() {
        posX = shell.getDisplay().getBounds().width - shell.getSize().x;
        posY = shell.getDisplay().getBounds().height - 30;
        shell.setLocation(posX, posY);
        int p = shell.getSize().y;
        while (p > 0) {
          p -= 10;
          posY -= 10;
          shell.setLocation(posX, posY);
          try {
            Thread.sleep(10);
          } catch (InterruptedException ie) {
          }
        }

        autoClose();
      }

      private void autoClose() {
        if (closeTime > 0) {
          try {
            Thread.sleep(closeTime * 1000);
          } catch (InterruptedException ie) {
          } finally {
            shell.close();
          }
        }
      }
    });
  }

}
